package com.qlm.config;



import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.SQLException;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import com.jfinal.config.Constants;
import com.jfinal.config.Handlers;
import com.jfinal.config.Interceptors;
import com.jfinal.config.JFinalConfig;
import com.jfinal.config.Plugins;
import com.jfinal.config.Routes;
import com.jfinal.ext.handler.ContextPathHandler;
import com.jfinal.ext.interceptor.SessionInViewInterceptor;
import com.jfinal.ext.plugin.quartz.QuartzPlugin;
import com.jfinal.kit.PropKit;
import com.jfinal.plugin.activerecord.ActiveRecordPlugin;
import com.jfinal.plugin.c3p0.C3p0Plugin;
import com.jfinal.plugin.ehcache.EhCachePlugin;
import com.jfinal.render.ViewType;
import com.qlm.cache.CacheManmager;
import com.qlm.cache.MsgCache;
import com.qlm.cache.RankCache;
import com.qlm.cache.ShopCache;
import com.qlm.controller.CacheController;
import com.qlm.controller.GameController;
import com.qlm.controller.LoginController;
import com.qlm.controller.MainController;
import com.qlm.controller.MyMsgController;
import com.qlm.controller.WeixinPayController;
import com.qlm.entity.Address;
import com.qlm.entity.Comment;
import com.qlm.entity.Gift;
import com.qlm.entity.Image;
import com.qlm.entity.Order;
import com.qlm.entity.Rank;
import com.qlm.entity.ShopCar;
import com.qlm.entity.User;
import com.qlm.entity.Yhq;
import com.qlm.log.LogerFactory;


public class JFINALConfig extends JFinalConfig{
	protected final static Logger       logger = LoggerFactory.getLogger(JFINALConfig.class); 
	
	@Override
	public void configConstant(Constants me) {
		PropKit.use("config.properties");
		me.setDevMode(PropKit.getBoolean("devMode", true));	
		me.setLogFactory(new LogerFactory());
		me.setViewType(ViewType.JSP);
		me.setError404View("/wx/500.jsp");
		me.setError500View("/wx/500.jsp");
	}

	@Override
	public void configRoute(Routes me) {
		me.add("/", MainController.class);
		me.add("/pay", WeixinPayController.class);
		me.add("/cache",CacheController.class);
		me.add("/msg",MyMsgController.class);
		me.add("/game",GameController.class);
		me.add("/login",LoginController.class);
	}
			
		
	

	@Override
	public void configPlugin(Plugins me) {
		// 配置C3p0数据库连接池插件
		C3p0Plugin c3p0Plugin = new C3p0Plugin(PropKit.get("jdbcUrl"), PropKit.get("user"), PropKit.get("password").trim());
		me.add(c3p0Plugin);
		// 配置e插件
		me.add(new EhCachePlugin());
		
		// 配置ActiveRecord插件
		ActiveRecordPlugin arp = new ActiveRecordPlugin(c3p0Plugin);
		me.add(arp);
		//表对模型映射
		checkDataBase();
		arp.addMapping("t_car", ShopCar.class);
		arp.addMapping("t_gift", Gift.class);
		arp.addMapping("t_order", Order.class);
		arp.addMapping("t_img", Image.class);
		arp.addMapping("t_comment", Comment.class);	
		arp.addMapping("t_user","openid", User.class);
		arp.addMapping("t_rank","openid", Rank.class);
		arp.addMapping("t_yhq", Yhq.class);	
		arp.addMapping("t_addr", Address.class);	
		QuartzPlugin quartzPlugin = new QuartzPlugin("job.properties");
		me.add(quartzPlugin);
		arp.setShowSql(PropKit.getBoolean("devMode", true));
		arp.setDevMode(PropKit.getBoolean("devMode", true));
	}

	/**
	 * 检查数据库，是否能连接上并且获取 当前数据库名
	 */
	private void checkDataBase() {
		Connection con = null;
		try {
			Class.forName("com.mysql.jdbc.Driver");
			con = DriverManager.getConnection(PropKit.get("jdbcUrl"), PropKit.get("user"), PropKit.get("password")
					.trim());
		} catch (Exception se) {
			se.printStackTrace();
			logger.error("数据库连接失败！");
			throw new RuntimeException(se);
		} finally {
			if (con != null) {
				try {
					con.close();
				} catch (SQLException e) {
					e.printStackTrace();
				}
			}
		}
	}

	@Override
	public void configInterceptor(Interceptors me) {
		// TODO Auto-generated method stub
		me.add(new SessionInViewInterceptor());
	}

	@Override
	public void configHandler(Handlers me) {
		// TODO Auto-generated method stub
		//添加根目录路径
		me.add(new ContextPathHandler("ctx")); 
	}

	@Override
	public void afterJFinalStart() {
		CacheManmager.register("shop", new ShopCache());
		MsgCache.update();
		RankCache.init();
	}

	
}
